package com.japp.manager.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.japp.base.BaseMapper;
import com.japp.manager.bean.CustomerBean;
import com.japp.manager.bean.CustomerInfoBean;
import com.japp.manager.mapper.CustomerMapper;
import com.japp.sys.bean.SelectBean;
import com.japp.sys.service.SelectService;
import com.japp.tools.common.Common;
import com.japp.tools.common.JappLog;
import com.japp.tools.page.Page;

@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerService implements BaseMapper<CustomerBean> {

	@Autowired(required=true)
	private CustomerMapper customerMapper;
	@Autowired
	private SelectService selectService;
	/**
	 * 客户更新新增查询下拉数据
	 * @return
	 */
	public Map<String, String> yinserCustomer(){
		Map<String, String> map = new HashMap<String, String>();
		List<SelectBean> list = null;
		String str;
		try {
			for (int i = 0; i <= 11; i++) {
				list = selectService.querySelectByTypeToJson(String.valueOf(i));
				str = Common.formtSelectToJson(list);
				map.put("sel_"+i, str);
			}
		} catch (Exception e) {
			JappLog.log.error("客户更新新增查询下拉数据错误:",e);
			throw new RuntimeException();
		}
		return map;
	}


	public CustomerBean get(Serializable pk) {
		return null;
	}

	public Page<CustomerBean> getPage(Page<CustomerBean> po, CustomerBean obj) {
		Page<CustomerBean> page = null;
		try {
			page = customerMapper.getPage(po, obj);
		} catch (Exception e) {
			JappLog.log.error("", e);
			throw new RuntimeException();
		}
		return page;
	}

	public CustomerBean findCustomerById(int id) {
		CustomerBean rel = null;
		try {
			rel = customerMapper.findCustomerById(id);
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return rel;
	}

	/**
	 * 新增计划或者服务。
	 * @param c
	 * @param lci
	 * @return
	 */
	public int iinsertCustomerAndInfo(CustomerBean c,List<CustomerInfoBean> lci){
		int rel = 0,c_id = 0;
		try {
			rel = customerMapper.insertCustomer(c);
			c_id = c.getC_id();
			if(c_id==0){
				System.out.println("失败");
			}else{
				JappLog.log.info("客户添加成功:"+c.getC_name());
				if(lci!=null&&lci.size()>0){
					rel = customerMapper.insertCustomerInfo(c_id, lci);
					JappLog.log.info("客户计划或服务添加成功:"+rel);
				}
			}
			
		} catch (Exception e) {
			JappLog.log.error("",e);
			throw new RuntimeException("新增失败事物回滚");
		}
		
		return rel;
	}
	
	
	public int insertCustomer(CustomerBean customer) {
		try {
			int rel = customerMapper.insertCustomer(customer);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	@Transactional(rollbackFor = Exception.class)
	public int deleteCustomerById(String[] keys) {
		int rel = 0;
		try {
			if(keys!=null&&keys.length>0){
				rel = customerMapper.deleteCustomerById(keys);
				rel = customerMapper.deleteCustomerInfoByCid(keys);
			}
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
			throw new RuntimeException("新增失败事物回滚");
		}
	}
	@Transactional(rollbackFor = Exception.class)
	public int updateCustomer(CustomerBean c,List<CustomerInfoBean> lci,List<CustomerInfoBean> lci1) {
		try {
			int rel = customerMapper.updateCustomer(c);
			if(rel>0){
				if(lci!=null&&lci.size()>0){
					rel = customerMapper.insertCustomerInfo(c.getC_id(), lci);
					JappLog.log.info("客户计划或服务添加成功:"+rel);
				}
				if(lci1!=null&&lci1.size()>0){
					for(CustomerInfoBean cs:lci1)
					rel = customerMapper.updateCustomerInfo(cs);
				}
				if("3".equals(c.getC_status())||"4".equals(c.getC_status())){
					customerMapper.deleteCustomerInfoTypeByCid(c.getC_id(),1);
				}
			}
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
			throw new RuntimeException("新增失败事物回滚");
		}
	}

	public List<CustomerInfoBean> findCustomerInfoByCId(int id) {
		try {
			List<CustomerInfoBean> list = customerMapper
					.findCustomerInfoByCId(id);
			return list;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public List<CustomerInfoBean> findCustomerInfoById(int id) {
		try {
			List<CustomerInfoBean> list = customerMapper
					.findCustomerInfoById(id);
			return list;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public int deleteCustomerInfoByCid(String[] id) {
		try {
			int rel = customerMapper.deleteCustomerInfoByCid(id);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	public int deleteCustomerInfoById(String[] keys) {
		try {
			int rel = customerMapper.deleteCustomerInfoById(keys);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

}
